package cn.openea.perpartner.meily.web.service.impl;

import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.meily.util.UserContext;
import cn.openea.perpartner.meily.web.entity.GoodsSupplyChain;
import cn.openea.perpartner.meily.web.entity.vo.GoodsSupplyChainEditVo;
import cn.openea.perpartner.meily.web.mapper.GoodsSupplyChainMapper;
import cn.openea.perpartner.meily.web.service.IGoodsSupplyChainService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;


@Service("goodsSupplyChainService")
public class GoodsSupplyChainServiceImpl extends ServiceImpl<GoodsSupplyChainMapper, GoodsSupplyChain> implements IGoodsSupplyChainService {
    @Override
    public List<GoodsSupplyChain> getListByParentId(Long parentId) {
        return baseMapper.selectList(new LambdaQueryWrapper<GoodsSupplyChain>().eq(GoodsSupplyChain::getParentId, parentId));
    }

    @Override
    public void edit(GoodsSupplyChainEditVo editVo) {
        if (editVo.getId() == null) {
            throw new ServiceException("数据不存在");
        }
        GoodsSupplyChain oldGoodsSupplyChain = Optional.ofNullable(getById(editVo.getId()))
                .orElseThrow(() -> new ServiceException("数据不存在"));
        if (!editVo.getParentId().equals(oldGoodsSupplyChain.getParentId()) && editVo.getParentId() != 0) {
            // 说明更改的类目 需要判断类目是否存在
            Optional.ofNullable(getById(editVo.getParentId()))
                    .orElseThrow(() -> new ServiceException("该供应链父类不存在"));
        }
        // 判断同一个供应链下是否存在相同名称的数据
        if (existence(editVo.getName(), editVo.getParentId())) {
            throw new ServiceException(editVo.getName() + "已存在");
        }
        GoodsSupplyChain goodsSupplyChain = new GoodsSupplyChain();
        BeanUtils.copyProperties(editVo, goodsSupplyChain);
        goodsSupplyChain.setUpdateUser(UserContext.getUserId());
        updateById(goodsSupplyChain);
    }

    @Override
    public void add(GoodsSupplyChainEditVo editVo) {
        if (editVo.getParentId() != 0) {
            Optional.ofNullable(getById(editVo.getParentId()))
                    .orElseThrow(() -> new ServiceException("该供应链父类别不存在"));
            // 判断同一个供应链下是否存在相同名称的数据
            if (existence(editVo.getName(), editVo.getParentId())) {
                throw new ServiceException(editVo.getName() + "已存在");
            }
        }
        GoodsSupplyChain goodsSupplyChain = new GoodsSupplyChain();
        goodsSupplyChain.setParentId(editVo.getParentId());
        goodsSupplyChain.setCreateUser(UserContext.getUserId());
        goodsSupplyChain.setName(editVo.getName());
        goodsSupplyChain.setUpdateUser(UserContext.getUserId());
        baseMapper.insert(goodsSupplyChain);
    }

    @Override
    public boolean existence(String name, Long parentId) {
        return baseMapper.existenceData(parentId, name) != null;
    }
}